package com.xiaoniu.forum.web.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.forum.annotation.Login;
import com.xiaoniu.forum.annotation.LoginUser;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.R;
import com.xiaoniu.forum.entity.MemberEntity;
import com.xiaoniu.forum.entity.vo.AppFollowFansNumVo;
import com.xiaoniu.forum.entity.vo.AppUserFollowStatusVo;
import com.xiaoniu.forum.service.MemberFollowService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/follow")
public class AppFollowController {
    @Autowired
    private MemberFollowService followService;
    /**
     *
     * 关注用户
     */
    @PostMapping("/follow/{id}")
    @ApiOperation("关注用户")
    @Login
    public R follow(@LoginUser MemberEntity user, @PathVariable String id){
        followService.follow(user.getId(),id);
        return R.ok();
    }

    /**
     * 取消关注
     */
    @PostMapping("/cancelFollow/{id}")
    @ApiOperation("取消关注")
    @Login
    public R cancelFollow(@LoginUser MemberEntity user, @PathVariable String id){
        followService.cancelFollow(user.getId(),id);
        return R.ok();
    }

    /**
     * 根据用户id，查询他的关注数、粉丝数
     */
    @GetMapping("/followFansNum/{id}")
    @ApiOperation("根据用户id，查询他的关注数、粉丝数")
    public R followFansNum(@PathVariable String id){
        AppFollowFansNumVo followFansNum = followService.queryFollowFansNum(id);
        return R.ok().put("data",followFansNum);
    }


    /**
     *  根据用户id。分页查询该用户的粉丝列表(只返回粉丝用户id)
     */
    @PostMapping("/fans/list/{userId}")
    @ApiOperation("根据用户id。查询分页查询该用户的粉丝列表，只返回粉丝用户id")
    public R listFans(@PathVariable String userId,@RequestBody Page page){
        PageUtils pageUtils = followService.listFans(userId,page);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 根据用户id。分页查询该用户的关注列表(只返回关注的用户id)
     */
    @PostMapping("/follows/list/{userId}")
    @ApiOperation("根据用户id。查询分页查询该用户关注列表，只返回关注的用户id")
    public R listFollows(@PathVariable String userId,@RequestBody Page page){
        PageUtils pageUtils = followService.listFollows(userId,page);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 查询当前用户是否已关注了另一个用户
     */
    @GetMapping("/isFollow/{id}")
    @ApiOperation("查询当前用户是否已关注了另一个用户")
    @Login
    public R isFollow(@LoginUser MemberEntity user,@PathVariable String id){
        boolean status = followService.isFollow(user.getId(),id);
        return R.ok().put("status",status);
    }

    /**
     * 批量查询当前用户是否已关注了另一批用户
     */
    @PostMapping("/batch/isFollow")
    @ApiOperation("查询当前用户是否已关注了另一批用户")
    @Login
    public R batchQueryIsFollow(@LoginUser MemberEntity user,@RequestBody List<String> ids){
        List<AppUserFollowStatusVo> list = followService.batchQueryIsFollow(user.getId(),ids);
        return R.ok().put("data",list);
    }
}
